package br.ufrj.management.pages.virtual;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class LogWriter {
	private static FileWriter log_file_stream;
	private static BufferedWriter log_file_interface;
	private static String log_file_path;
	
	public static void initialize(String filename){	
		log_file_path = filename;

	}
	
	public static void write_line_to_log(String line){
		
		try {
			log_file_interface.write(line);
			log_file_interface.newLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void write_array_list_string_to_log(ArrayList<String> collection){
		try{
			for(int i= 0; i < collection.size(); i++){
				log_file_interface.write(collection.get(i));
				log_file_interface.newLine();
			}
			
		} catch(IOException e){
			e.printStackTrace();
		}
		
	}
	
	public static void memory_dump(ArrayList<String> memory){
		
		try{
			log_file_interface.write("Dump da Memoria Principal: ");
			log_file_interface.newLine();
			log_file_interface.write("==========================================");
			log_file_interface.newLine();
			for(int i= 0; i < memory.size(); i++){
				log_file_interface.write("Frame "+ i + ": \t"+ memory.get(i));
				log_file_interface.newLine();
			}
			log_file_interface.write("==========================================");
			log_file_interface.newLine();
			log_file_interface.newLine();
			
		} catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static void open_log_file(){
		
		try {
			log_file_stream = new FileWriter(log_file_path, true);
			log_file_interface = new BufferedWriter(log_file_stream);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void close_log_file(){
		
		try {
			log_file_interface.close();
			log_file_stream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void write_safe_line_to_log(String line){
		open_log_file();
		write_line_to_log(line);
		close_log_file();
	}

	public static void safe_memory_dump(ArrayList<String> memory){
		open_log_file();
		
		try{
			log_file_interface.write("Dump da Memoria Principal: ");
			log_file_interface.newLine();
			log_file_interface.write("==========================================");
			log_file_interface.newLine();
			for(int i= 0; i < memory.size(); i++){
				log_file_interface.write("Frame "+ i + ": \t"+ memory.get(i));
				log_file_interface.newLine();
			}
			log_file_interface.write("==========================================");
			log_file_interface.newLine();
			log_file_interface.newLine();
			
		} catch(IOException e){
			e.printStackTrace();
		}
		close_log_file();
	}

}
